//
//  DockItem.m
//  新浪微博
//
//  Created by apple on 13-10-27.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "DockItem.h"

// 文字的高度比例
#define kTitleRatio 0.3
// 文字偏移
#define kTitleOffset -3

@implementation DockItem

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 1.文字居中
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        
        // 2.文字大小
        self.titleLabel.font = [UIFont systemFontOfSize:12];
        
        // 3.图片的内容模式
        self.imageView.contentMode = UIViewContentModeCenter;
        
        // 状态不改变图片
        self.adjustsImageWhenHighlighted = NO;
        self.adjustsImageWhenDisabled = NO;
        
        _titleRatio = kTitleRatio;
        _titleOffset = kTitleOffset;
    }
    return self;
}

#pragma mark 调整内部ImageView的frame
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
    if(![self titleForState:UIControlStateNormal] && ![self titleForState:UIControlStateHighlighted] && ![self titleForState:UIControlStateDisabled] && ![self titleForState:UIControlStateSelected]) {
        _titleRatio = 0;
    }
    
    CGFloat imageX = 0;
    CGFloat imageY = 0;
    CGFloat imageWidth = contentRect.size.width;
    CGFloat imageHeight = contentRect.size.height * ( 1- _titleRatio );
    return CGRectMake(imageX, imageY, imageWidth, imageHeight);
}


#pragma mark 调整内部UILabel的frame
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    if(![self imageForState:UIControlStateNormal] && ![self imageForState:UIControlStateHighlighted] && ![self imageForState:UIControlStateDisabled] && ![self imageForState:UIControlStateSelected]) {
        _titleRatio = 1;
    }
    CGFloat titleX = 0;
    CGFloat titleHeight = contentRect.size.height * _titleRatio;
    CGFloat titleY = contentRect.size.height - titleHeight + _titleOffset;
    CGFloat titleWidth = contentRect.size.width;
    return CGRectMake(titleX, titleY, titleWidth, titleHeight);
}


-(void)setHighlighted:(BOOL)highlighted {

}


@end

